/**
 * %W% %E%
 *
 * Copyright (c) 2012-3-23, lysong01@gmail.com. All rights reserved.
 */
package question;

import java.io.BufferedReader;
//import java.io.DataInputStream;
//import java.io.DataOutputStream;
//import java.io.EOFException;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author  liyasong
 * @version %I%, %G%
 * @since   liyasong1.0
 */
public class AboutIO {

	/**
	 * @param args
	 * void
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		File file = new File("newfile.txt");			//这一步只会申明文件名，并不会实际创建
		OutputStream os = new FileOutputStream(file);	//一旦创建输出流，文件就会创建
		String str = "I'm liyasong!\nwho are you?";
		os.write(str.getBytes());
		System.out.println(file.getAbsolutePath());		//文件创建路径在工程目录下，为什么呢？
		
		InputStream is = new FileInputStream(file);
		int i;
		while ((i=is.read()) != -1) {
			System.out.print((char)i);
		}
		System.out.println();
//		byte [] barray = new byte[(int)file.length()];	//可见此种读取方法并不合适
//		is.read(barray);
//		System.out.println(Arrays.toString(barray));
/*		
		DataOutputStream dos = new DataOutputStream(new FileOutputStream(file));
		dos.writeByte(48);								//一旦创建dos，dis就只读dos写入的数据
														//可见他们使用起来最好成对使用
		try {
			DataInputStream dis = new DataInputStream(new FileInputStream(file));
			while (true) {
				System.out.print((char)dis.readByte());	//因为stream都是处理字节的，文件的每个字符
			}											//以字节保存的，用readChar()反而读不出来
		} catch (EOFException e) {
			System.err.println("end of stream");
		}
*/	
		BufferedWriter bw = new BufferedWriter(new FileWriter(file));
		bw.write("\nthis line is written by bw");
		
		BufferedReader br = new BufferedReader(new FileReader(file));
		String str1;
		while((str1=br.readLine())!=null){
			System.out.println(str1);
		}
	}

}
